#include "ElementComponent.h"
#include "ContainerComponent.h"
#include "../SimpleDockApplication.h"

ElementComponent::ElementComponent (ContainerComponent * owner_, DockElement * delem)
: mouseIn(false),
  owner (owner_),
  dockElement (delem)
{
	addAndMakeVisible(imageHolder = new ImageHolder(delem->getImageToDraw()));

	setSize(imageHolder->getWidth() + 5, imageHolder->getHeight() + 5);

	if(dockElement->getType() == container)
		childContainer = new ContainerComponent(this, dockElement);
	else
		childContainer = 0;

	this->setInterceptsMouseClicks(false, false);
}
    
ElementComponent::~ElementComponent()
{
	deleteAllChildren();

	if(childContainer->isValidComponent())
		delete childContainer;
}

ImageHolder * ElementComponent::getImageHolder()
{
	return imageHolder;
}

ContainerComponent * ElementComponent::getParentContainer()
{
	return owner;
}

ContainerComponent * ElementComponent::getChildContainer()
{
	return childContainer;
}

String ElementComponent::getTexttoShow()
{
	return dockElement->getTitle();
}

void ElementComponent::paint (Graphics& g)
{
}
    
void ElementComponent::resized()
{
	int w = 0;
	int h = 0;
	if(imageHolder->isValidComponent())
	{
		w = imageHolder->getWidth() + 10;
		h = imageHolder->getHeight() + 10;
	}
	//setSize(w, h);
	
	int x = getWidth() /2 - imageHolder->getWidth() / 2;
	imageHolder->setTopLeftPosition(x, 5);

	imageHolder->setSize(getWidth() - 5, getHeight() - 5);
	//owner->resized();
}

void ElementComponent::mouseEnter(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseEnterElement(this, e);
}

void ElementComponent::mouseExit(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseExitElement(this, e);
}

void ElementComponent::mouseMove(const MouseEvent& e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseMoveOnElement(this, e);
}

void ElementComponent::mouseDrag(const MouseEvent &e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseDragElement(this, e);
}

void ElementComponent::mouseUp(const MouseEvent &e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseUp(this, e);
}

void ElementComponent::mouseDown(const MouseEvent &e)
{
	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseDownOnElement(this, e);
}

DockElement * ElementComponent::getDockElement()
{
	return dockElement;
}